/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.validator.engine;

import java.util.*;
import ch.trackedbean.validator.*;

/**
 * Configuration for an {@link IPropertyValidator}.
 * 
 * @author M. Hautle
 */
public class Configuration {
	/** Map holding the configuration values. */
	private Map<String, Object> values = new HashMap<String, Object>();

	/**
	 * Adds a configuration entry.
	 * 
	 * @param key The key
	 * @param value The value
	 */
	public void addEntry(String key, Object value) {
		values.put(key, value);
	}

	/**
	 * Clears the configuration content.
	 */
	public void clear() {
		values.clear();
	}

	/**
	 * Returns the configuration attribute as string value.
	 * 
	 * @param name The name of the value
	 * @return The value or null
	 */
	public Object getObject(String name) {
		return values.get(name);
	}

	/**
	 * Returns the configuration attribute as string value.
	 * 
	 * @param name The name of the value
	 * @param undefined Value for an undefined entry
	 * @return The value
	 */
	public String getString(String name, String undefined) {
		final Object val = values.get(name);
		return val != null ? val.toString() : undefined;
	}

	/**
	 * Returns the configuration attribute as boolean value.
	 * 
	 * @param name The name of the value
	 * @param undefined Value for an undefined entry
	 * @return The value
	 */
	public boolean getBoolean(String name, boolean undefined) {
		final Object val = values.get(name);
		if (val == null)
			return undefined;
		if (val instanceof Boolean)
			return ((Boolean) val).booleanValue();
		return Boolean.parseBoolean(val.toString());
	}

	/**
	 * Returns the configuration attribute as integer value.
	 * 
	 * @param name The name of the value
	 * @param undefined Value for an undefined entry
	 * @return The value
	 */
	public int getInt(String name, int undefined) {
		final Number n = getNumber(name, null);
		return n != null ? n.intValue() : undefined;
	}

	/**
	 * Returns the configuration attribute as long value.
	 * 
	 * @param name The name of the value
	 * @param undefined Value for an undefined entry
	 * @return The value
	 */
	public long getLong(String name, long undefined) {
		final Number n = getNumber(name, null);
		return n != null ? n.longValue() : undefined;
	}

	/**
	 * Returns the configuration attribute as double value.
	 * 
	 * @param name The name of the value
	 * @param undefined Value for an undefined entry
	 * @return The value
	 */
	public double getDouble(String name, double undefined) {
		final Number n = getNumber(name, null);
		return n != null ? n.doubleValue() : undefined;
	}

	/**
	 * Returns the configuration attribute as {@link Number}.
	 * 
	 * @param name The name of the value
	 * @param undefined Value for an undefined entry
	 * @return The value
	 */
	public Number getNumber(String name, Number undefined) {
		final Object val = values.get(name);
		if (val == null)
			return undefined;
		if (val instanceof Number)
			return (Number) val;
		return Double.valueOf(val.toString());
	}
}
